#!/bin/bash

export LC_ALL=C

GA_DIR=$1

GENE_ID=`head -n1 $GA_DIR/gene.faa | cut -b 2-`
GENE_PATHWAY=`join -t$'\t' <(echo $GENE_ID) /home/mixagol/data/5_pathways/gene_pathway_map.txt | cut -f2 | head -n1`

echo -ne $(basename $GA_DIR)'\t'$GENE_PATHWAY

for E_DIR in $GA_DIR/e_*; do
    
    BEST_PATHWAY=`cat $E_DIR/results_pathways.txt \
        | awk -F'\t' 'BEGIN{CUR_P=0; NUM_P=0}{if (CUR_P==0) {CUR_P=$1; MIN_P=CUR_P}; if ($1 > CUR_P) {NUM_P = NUM_P+1; CUR_P=$1}; if (NUM_P<1) print}' \
        | cut -f2 | sort -t$'\t' | uniq -c | sort -k1,1 -nr | head -n1`

    if [ "$(echo $BEST_PATHWAY | cut -d' ' -f2-)" == "$GENE_PATHWAY" ] ; then
        EQUAL=1
    else
        if [ "x$GENE_PATHWAY" == "x" ] ; then
            EQUAL=2
        else
            EQUAL=0
        fi
    fi

    echo -ne '\t'$EQUAL'\t| '$BEST_PATHWAY

done

echo

